<?php
require(dirname(dirname(__FILE__)) ."/php_sdk-master/mobpush/push.php");
use mobpush\Push as mobPush;
date_default_timezone_set("PRC");
/**
 * 极光消息推送服务
 * 此消息推送使用别名指定推送
 * 推送到指定用户需要手机端设置用户别名 alias
 * 推送到相应平台需要手机端将用户平台设置为统一的tag 从而实现多平台发送
 * @copyright (c) 2018, gandianli
 * @author sunman
 */
class service_mobpush extends gdl_components_baseservice
{
    # 极光平台配置参数 app与服务配置需一致
	private static $appKey = "26ebe97da8c5a";
    private static $secret = "3ce64aa565622093573ca3b8ccea5cbb";

    # 需要发送的手机系统 选择相应手机系统发送 默认发送所有 1、android ； 2、ios ；如包含ios和android则为[1,2]
    public  $systems = [1,2]; 
	
    #默认推送所有平台 如果需要推送到相应平台请改值为数组['平台1','平台2'] 前提条件为app已经设置用户的tags为相应平台名
    public  $company = 'all';

    public function __construct($company="all",$systems=[1,2])
    {
        parent::__construct();
        # 初始化
        if(!empty($systems)){
            $this->systems = $systems;
        }
        if(!empty($company)){
            $this->company = $company;
        }
    }
	
	/**
	* 通知栏消息 自定义消息 
	* @param content 消息内容 必填可以为空
	* @param target	推送范围:1广播；2别名；3标签；4regid；5地理位置;6用户分群 默认使用别名单个推送 必填不可为空
	* @param userId alias 和 tags 集合【】 如果target =2 则为alias用户别名 如果target = 3 则为标签集合 多个用户用逗号分隔 必填不可为空
	* @param type type=1通知栏 type=2自定义 必填不可以为空
	* @param extras extras = [key=>value, key=>value] 键值对提供参数 可选
	* @param text 自定义消息体 可选
	*/
	public function pushAllNotice($content = "", $target=2, $userId = "", $type=1, $extras=[], $text=""){
		
		if(empty($content)){return false;}
		$isTarget = [1,2,3,4,5,6];
		
		# cate 1发送推送 2设置别名 3设置标签
		$cate = 1;
		
		#拼接request 消息体数据集
		$request['plats'] = $this->systems; 
		$request['target'] = in_array($target, $isTarget) ? $target : 2; 
		$request['content'] = $content; 
		$request['type'] = $type?$type:1; 
		$request['extras'] = $extras;
		
		#安卓提示音
		$request['androidVoice'] = true;
		$request['androidShake'] = true;
		$request['androidLight'] = true;
		
		#自定义消息还是通知消息 推送类型：1通知；2自定义
		if($type==2){
			$request['customTitle'] = $content;
			$request['customType'] = $text?$text:"";
		}
		
		#类型
		$target = intval($target);
		switch($target){
			case 1:
			//$request['alias'] = $userId;
			break;
			
			case 2:
				$alias = explode(",", $userId);
				$request['alias'] = $alias;
			break;
			
			case 3:
				$tags = explode(",", $userId);
				$request['tags'] = $tags;
			break;
			
			case 4:
				$registrationIds = explode(",", $userId);
				$request['registrationIds'] = $registrationIds;
			break;
			
			case 5:
				$request['city'] = $userId;
			break;
			
			case 6:
				$request['block'] = $userId;
			break;
			
			default:
				$alias = explode(",", $userId);
				$request['alias'] = $alias;
			break;
		}
		 
		# 实例化mobpush对象
		$pushObject = new mobPush(self::$appKey, self::$secret);
		
		#发送消息
		$pushResult = $pushObject->postPush($request, $cate);
		
		var_dump($pushResult);exit;	
		
	}
	
	/**
		设置别名
	*/
	public function setAlias($alias = "", $registrationId = ""){
		if(empty($alias)){return false;}
		//if(empty($registrationId)){return false;}
		
		#拼接request 消息体数据集
		$request['plats'] = $this->systems; 
		$request['target'] = 2; 
		$request['content'] = 'ceshi'; 
		$request['type'] = 1; 
		$request['alias'] = $alias;
		$request['registrationId'] = $registrationId;
		
		# 实例化mobpush对象
		$pushObject = new mobPush(self::$appKey, self::$secret);
		#设置别名
		$pushResult = $pushObject->postPush($request, 2);
		var_dump($pushResult);exit;
		
	}
	/**
	*/
	public function setTags($tags = ""){
		if(empty($tags)){return false;}
		#拼接request 消息体数据集
		$request['plats'] = $this->systems; 
		$request['target'] = 3; 
		$request['content'] = '1221'; 
		$request['type'] = 1;  
		$request['tags'] = $tags;
		$request['registrationId'] = $registrationId;
		
		# 实例化mobpush对象
		$pushObject = new mobPush(self::$appKey, self::$secret);
		#设置别名
		$pushResult = $pushObject->postPush($request, 2);
		var_dump($pushResult);exit;
	}

    

}